﻿using System;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using AZROLESLib;

namespace AzManContrib.MVC.Html
{
    public static class EditorExtensions
    {

        /* 
         * Non generic with operation by name
         */
        public static MvcHtmlString EditorFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string operation)
        {
            return html.EditorFor(expression, AzMan.Application.Operations.FindByName(operation));
        }

        public static MvcHtmlString EditorFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object additionalViewData, string operation)
        {
            return html.EditorFor(expression, additionalViewData, AzMan.Application.Operations.FindByName(operation));
        }

        public static MvcHtmlString EditorFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string templateName, string operation)
        {
            return html.EditorFor(expression, templateName, AzMan.Application.Operations.FindByName(operation));
        }

        public static MvcHtmlString EditorFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string templateName, object additionalViewData, string operation)
        {
            return html.EditorFor(expression, templateName, additionalViewData, AzMan.Application.Operations.FindByName(operation));
        }

        public static MvcHtmlString EditorFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string templateName, string htmlFieldName, string operation)
        {
            return html.EditorFor(expression, templateName, htmlFieldName, AzMan.Application.Operations.FindByName(operation));
        }

        public static MvcHtmlString EditorFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string templateName, string htmlFieldName, object additionalViewData, string operation)
        {
            return html.EditorFor(expression, templateName, htmlFieldName, additionalViewData, AzMan.Application.Operations.FindByName(operation));
        }


        /* 
         * Non generic method, as there is no sense to set a css class on an editor without knowing it's template.
         */
        public static MvcHtmlString EditorFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, IAzOperation operation)
        {
            return !HttpContext.Current.User.Can(operation) ? MvcHtmlString.Empty : html.EditorFor(expression);
        }

        public static MvcHtmlString EditorFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object additionalViewData, IAzOperation operation)
        {
            return !HttpContext.Current.User.Can(operation) ? MvcHtmlString.Empty : html.EditorFor(expression, additionalViewData);
        }

        public static MvcHtmlString EditorFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string templateName, IAzOperation operation)
        {
            return !HttpContext.Current.User.Can(operation) ? MvcHtmlString.Empty : html.EditorFor(expression, templateName);
        }

        public static MvcHtmlString EditorFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string templateName, object additionalViewData, IAzOperation operation)
        {
            return !HttpContext.Current.User.Can(operation) ? MvcHtmlString.Empty : html.EditorFor(expression, templateName, additionalViewData);
        }

        public static MvcHtmlString EditorFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string templateName, string htmlFieldName, IAzOperation operation)
        {
            return !HttpContext.Current.User.Can(operation) ? MvcHtmlString.Empty : html.EditorFor(expression, templateName, htmlFieldName);
        }

        public static MvcHtmlString EditorFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string templateName, string htmlFieldName, object additionalViewData, IAzOperation operation)
        {
            return !HttpContext.Current.User.Can(operation) ? MvcHtmlString.Empty : html.EditorFor(expression, templateName, htmlFieldName, additionalViewData);
        }
    }
}
